package com.jason.measure.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

/**
 * @DESC:   自定义viewgroup
 * @Author: Jason
 * @Date: 2016/10/17
 * @Time: 下午5:26
 */

public class CustomLayout3 extends ViewGroup {


    public CustomLayout3(Context context, AttributeSet attrs) {
        super(context, attrs);


    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if (getChildCount() > 0) {
            measureChildren(widthMeasureSpec, heightMeasureSpec);
        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {

        // 获取父容器的内边距
        int parentPaddingLeft = getPaddingLeft();
        int parentPaddingTop = getPaddingTop();


        // 判断是否有子元素
        if (getChildCount() > 0) {
            // 声明一个临时变量存储高度倍增值
            int mutilHeight = 0;

            // 遍历子元素并对其进行定位布局
            for (int i = 0; i < getChildCount(); i++) {
                // 获取每个子元素
                View childView = getChildAt(i);
                // 通知子元素进行布局  考虑父容器内边距的影响
                childView.layout(parentPaddingLeft, mutilHeight + parentPaddingTop, childView.getMeasuredWidth() + parentPaddingLeft, childView.getMeasuredHeight() + mutilHeight + parentPaddingTop);

                // 改变高度倍增值
                mutilHeight += childView.getMeasuredHeight();
            }
        }

    }
}
